package com.hsq.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.hsq.mapper.ClientRequestMapper;
import com.hsq.pojo.ClientRequest;
import com.hsq.pojo.PageBean;
import com.hsq.service.ClientRequestService;
import com.hsq.util.HttpDriver;
import com.hsq.util.SqlSessionFactoryUtils;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;

public class ClientRequestServiceImpl implements ClientRequestService {

    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 分页条件查询
     * 根据当前页码、页面条数、搜索条件返回PageBean页面对象
     * @param currentPage
     * @param pageSize
     * @param clientRequest
     * @return
     */
    @Override
    public PageBean<ClientRequest> selectByPageAndCondition(int currentPage, int pageSize, ClientRequest clientRequest) {
        // 计算开始索引
        int begin = (currentPage - 1) * pageSize;

        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        // 处理条件，模糊表达式
        String name = clientRequest.getName();
        if (name != null && name != "") {
            clientRequest.setName("%" + name + "%");
        }

        // 查询当前页数据和总记录数
        List<ClientRequest> rows = mapper.selectByPageAndCondition(begin, pageSize, clientRequest);
        int count = mapper.countByPageAndCondition(clientRequest);

//        System.out.println("rows"+rows);

        // 封装页面对象
        PageBean<ClientRequest> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(count);

        return pageBean;
    }


    /**
     * 根据id删除数据
     * @param id
     */
    @Override
    public void deleteById(int id) {
        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        // 调用方法删除数据
        mapper.deleteById(id);

        // 提交事务
        session.commit();

        // 释放资源
        session.close();
    }


    /**
     * 添加数据
     * @param clientRequest
     */
    @Override
    public void add(ClientRequest clientRequest) {
        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        // 调用方法添加数据
        mapper.add(clientRequest);

        // 提交事务
        session.commit();

        // 释放资源
        session.close();
    }


    /**
     * 根据id数组删除数据
     * @param ids
     */
    @Override
    public void deleteByIds(int[] ids) {
        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        // 调用方法
        mapper.deleteByIds(ids);

        // 提交事务
        session.commit();

        // 释放资源
        session.close();
    }


    /**
     * 根据id查找数据
     * @param id
     * @return
     */
    @Override
    public ClientRequest selectById(int id) {
        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        // 调用方法
        ClientRequest clientRequest = mapper.selectById(id);

        // 释放资源
        session.close();

        return clientRequest;
    }


    /**
     * 修改请求
     * @param clientRequest
     */
    @Override
    public void update(ClientRequest clientRequest) {
        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        // 调用方法
        mapper.update(clientRequest);

        // 提交事务
        session.commit();

        // 释放资源
        session.close();
    }



    @Override
    public String run(int id) {
        // 获取sqlSession和mapper
        SqlSession session = sqlSessionFactory.openSession();
        ClientRequestMapper mapper = session.getMapper(ClientRequestMapper.class);

        ClientRequest clientRequest = mapper.selectById(id);

        // 获取请求类型
        int requestType = clientRequest.getRequestType();
        // 获取请求链接
        String requestUrl = clientRequest.getRequestUrl();

        String responseBody;

        // 如果是get请求
        if (requestType == 1) {
            responseBody = HttpDriver.doGet(requestUrl);
        } else {
            // 如果是post请求
            int bodyType = clientRequest.getBodyType();
            // 获取请求体
            String body = clientRequest.getBody();

            // 如果是form类型的请求体
            if (bodyType == 1) {
                // 封装请求体
                List<NameValuePair> user = new ArrayList<>();
                String[] strings = body.split(";");
                for (String string : strings) {
                    String[] split = string.split(":");
                    user.add(new BasicNameValuePair(split[0].trim(),split[1].trim()));
                }
                // 获取响应
                responseBody = HttpDriver.doPost(requestUrl, user);
            } else {
                // 如果是json类型的请求体
                // 封装json请求体
                JSONObject object = new JSONObject();
                String[] strings = body.split(";");
                for (String string : strings) {
                    String[] split = string.split(":");
                    object.put(split[0].trim(),split[1].trim());
                }
                responseBody = HttpDriver.doPost(requestUrl, object);
            }
        }
        return responseBody;
    }
}
